package com.syqc.monitor.ui.tmn

import android.widget.ImageView
import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.base.BaseRecycleActivity
import com.syqc.comlib.rx.filterFast
import com.syqc.comlib.utils.GsonUtil
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.comlib.utils.loadGif
import com.syqc.comlib.utils.setResultTo
import com.syqc.comlib.utils.showToast
import com.syqc.entity.TmnManager
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivityChooseTmnBinding
import com.syqc.monitor.databinding.IncludeSelectSureBinding
import com.syqc.utils.Keys

/**
 *@author joker
 *@time   2024/3/11 16:51
 *@desc
 */
class ChooseTmnActivity :
    BaseRecycleActivity<ActivityChooseTmnBinding>(ActivityChooseTmnBinding::inflate) {
    private val mAdapter by lazy { ChooseTmnAdapter() }
    private lateinit var sureBinding: IncludeSelectSureBinding
    private val mViewModel by viewModels<DeleteTmnVM>()

    //空布局
    private val emptyView by lazy {
        val view = layoutInflater.inflate(
            R.layout.layout_empty_view,
            mRecyclerVB.recyclerView,
            false
        )
        view.findViewById<ImageView>(R.id.ivNoData).loadGif(this, R.mipmap.ic_empty)
        view
    }

    override fun initView() {
        super.initView()
        ToolbarHelper.setToolbar(this, R.string.title_choose_tmn)
        sureBinding = IncludeSelectSureBinding.bind(binding.root)
        sureBinding.tvSure.setText(R.string.desc_com_sure)
        mRecyclerVB.recyclerView.layoutManager = LinearLayoutManager(this)
        mRecyclerVB.recyclerView.adapter = mAdapter
        mViewModel.teamId = intent.getStringExtra(Keys.INTENT_TEAM_ID) ?: ""
        mViewModel.getTmnByTeam()
        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)
        mViewModel.tmnsLive.observe(this) {
            if (it.isNullOrEmpty()) {
                mAdapter.setEmptyView(emptyView)
                binding.flTmnTop.isVisible = false
            } else {
                binding.flTmnTop.isVisible = true
            }
            mAdapter.setList(it)
        }
    }

    override fun setListener() {


        mAdapter.setOnItemClickListener { adapter, view, position ->
            mAdapter.index = position
            mAdapter.notifyDataSetChanged()
        }

        sureBinding.tvSure.filterFast {
            if (mAdapter.index == -1) {
                showToast(R.string.toast_choose_tmn)
                return@filterFast
            }
            val tmnManager = mAdapter.data[mAdapter.index]
            setResultTo(this) {
                putExtra(Keys.INTENT_DATA, GsonUtil.toJson(tmnManager))
            }
            finish()
        }
    }
}


class ChooseTmnAdapter : BaseQuickAdapter<TmnManager, BaseViewHolder>(R.layout.item_choose_tmn) {
    var index = -1
    override fun convert(holder: BaseViewHolder, item: TmnManager) {
        item.run {
            holder.setText(R.id.tvTmnType, tmnKey)
                .setText(R.id.tvTmnSIM, simCard)
                .setText(R.id.tvTmnNum, tmnNo)
            holder.getView<ImageView>(R.id.ivTmnChoose).isSelected = holder.layoutPosition == index
        }
    }


}